package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.common.BaseContext;
import com.reggie.common.R;
import com.reggie.entity.ShoppingCart;
import com.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/*
购物车
 */

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

//  添加购物车
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //    设置用户id,指定当前 是那个用户的购物车数据
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        //    查询当前菜品或者套餐是否在购物车里
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
//            添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
//            添加的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
//        判断购物车里是否已经 有了相同的一条数据
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(queryWrapper);

//    如果存在,就在原来 数量基础上加1
         if (shoppingCartOne!=null){
             Integer number = shoppingCartOne.getNumber();
             shoppingCartOne.setNumber(number+1);
             shoppingCartService.updateById(shoppingCartOne);
         }else {
//    如果不存在,则添加到购物车,数量默认为1
             shoppingCart.setNumber(1);
             shoppingCart.setCreateTime(LocalDateTime.now());
          shoppingCartService.save(shoppingCart);
            shoppingCartOne=shoppingCart;
         }

         return R.success(shoppingCartOne);

    }

//    删除/减少购物车菜品
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(queryWrapper);
        Integer number = shoppingCartOne.getNumber();
        if (number>1){
            shoppingCartOne.setNumber(number-1);
            shoppingCartService.updateById(shoppingCartOne);
        }else {
            shoppingCartOne.setNumber(0);
            shoppingCartService.removeById(shoppingCartOne.getId());
        }
        shoppingCart=shoppingCartOne;
        return R.success(shoppingCart);
    }

//   购物车展示
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

//    清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("购物车清空成功");
    }
}
